/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.ai2.handler;

import com.aionengine.gameserver.ai2.AI2Logger;
import com.aionengine.gameserver.ai2.AIState;
import com.aionengine.gameserver.ai2.AISubState;
import com.aionengine.gameserver.ai2.NpcAI2;
import com.aionengine.gameserver.ai2.event.AIEventType;
import com.aionengine.gameserver.ai2.manager.AttackManager;
import com.aionengine.gameserver.ai2.manager.EmoteManager;
import com.aionengine.gameserver.ai2.manager.WalkManager;
import com.aionengine.gameserver.ai2.poll.AIQuestion;
import com.aionengine.gameserver.model.gameobjects.Creature;
import com.aionengine.gameserver.model.gameobjects.Npc;

/**
 * @author ATracer
 */
public class AttackEventHandler {

    /**
     * @param npcAI
     * @param creature
     */
    public static void onAttack(NpcAI2 npcAI, Creature creature) {
        if (npcAI.isLogging()) {
            AI2Logger.info(npcAI, "onAttack");
        }
        if (creature == null || creature.getLifeStats().isAlreadyDead()) {
            return;
        }
        // TODO lock or better switch
        if (npcAI.isInState(AIState.RETURNING)) {
            npcAI.getOwner().getMoveController().abortMove();
            npcAI.setStateIfNot(AIState.IDLE);
            npcAI.onGeneralEvent(AIEventType.NOT_AT_HOME);
            return;
        }
        if (!npcAI.canThink()) {
            return;
        }
        if (npcAI.isInState(AIState.WALKING)) {
            WalkManager.stopWalking(npcAI);
        }
        npcAI.getOwner().getGameStats().renewLastAttackedTime();
        if (!npcAI.isInState(AIState.FIGHT)) {
            npcAI.setStateIfNot(AIState.FIGHT);
            if (npcAI.isLogging()) {
                AI2Logger.info(npcAI, "onAttack() -> startAttacking");
            }
            npcAI.setSubStateIfNot(AISubState.NONE);
            npcAI.getOwner().setTarget(creature);
            AttackManager.startAttacking(npcAI);
            if (npcAI.poll(AIQuestion.CAN_SHOUT))
                ShoutEventHandler.onAttackBegin(npcAI, (Creature) npcAI.getOwner().getTarget());
        }
    }

    /**
     * @param npcAI
     */
    public static void onForcedAttack(NpcAI2 npcAI) {
        onAttack(npcAI, (Creature) npcAI.getOwner().getTarget());
    }

    /**
     * @param npcAI
     */
    public static void onAttackComplete(NpcAI2 npcAI) {
        if (npcAI.isLogging()) {
            AI2Logger.info(npcAI, "onAttackComplete: " + npcAI.getOwner().getGameStats().getLastAttackTimeDelta());
        }
        npcAI.getOwner().getGameStats().renewLastAttackTime();
        AttackManager.scheduleNextAttack(npcAI);
    }

    /**
     * @param npcAI
     */
    public static void onFinishAttack(NpcAI2 npcAI) {
        if (!npcAI.canThink()) {
            return;
        }
        if (npcAI.isLogging()) {
            AI2Logger.info(npcAI, "onFinishAttack");
        }
        Npc npc = npcAI.getOwner();
        EmoteManager.emoteStopAttacking(npc);
        npc.getLifeStats().startResting();
        npc.getAggroList().clear();
        if (npcAI.poll(AIQuestion.CAN_SHOUT))
            ShoutEventHandler.onAttackEnd(npcAI);
        npc.setTarget(null);
        npc.setSkillNumber(0);
    }
}
